﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using xnaMugen;

public class XnaMugen : MonoBehaviour {

    Mugen game;
    DrawQueue drawQueue;
    public float updateInterval = 0.5F;
    public static List<FileStream> files = new List<FileStream>();
    private float accum = 0; // FPS accumulated over the interval
    private int frames = 0; // Frames drawn over the interval
    private float timeleft; // Left time for current interval
    float fps;
    public Material mat;
    public static bool isOpenGlDraw;
    // Use this for initialization
    void Start()
    {
        Application.targetFrameRate = 60;
        isOpenGlDraw = OpenGlDraw;
        // Add an audio source and tell the media player to use it for playing sounds
        Microsoft.Xna.Framework.Media.MediaPlayer.AudioSource = gameObject.AddComponent<AudioSource>();

        drawQueue = new DrawQueue();
        game = new Mugen();
        game.DrawQueue = drawQueue;
        game.Begin();
        timeleft = updateInterval;
        //Application.targetFrameRate = 30;
    }

    // Update is called once per frame
    void Update()
    {
        DrawGame.instance.Refresh();
        float deltaTime = Time.deltaTime;
        if (deltaTime > 0.050f)
        {
            deltaTime = 0.050f;
        }
        //Debug.Log(deltaTime);
        game.Tick(deltaTime);
        drawQueue.Clear();

        timeleft -= Time.deltaTime;
        accum += Time.timeScale / Time.deltaTime;
        ++frames;

        // Interval ended - update GUI text and start new interval
        if (timeleft <= 0.0)
        {
            // display two fractional digits (f2 format)
            fps = accum / frames;

            //  DebugConsole.Log(format,level);
            timeleft = updateInterval;
            accum = 0.0F;
            frames = 0;
        }

    }
    bool a = false;

    private void OnApplicationQuit()
    {
        foreach (var file in files)
        {
            if (file != null)
                file.Close();
        }
    }

    private void DrawATriangle(Material mat)
    {
        for (int i = 0; i < DrawGame.instance.GlDraws.Count; i++)
        {
            Draw draw = DrawGame.instance.GlDraws[i];
            mat = draw.material;
            GL.PushMatrix();
            GL.LoadOrtho();

            GL.Begin(GL.TRIANGLES);
            //mat.SetPass(0);
            for (int v = 0; v < draw.Count; v++)
            {
                GL.Color(new Color32(draw.VertexData[v].Tint.R, draw.VertexData[v].Tint.G, draw.VertexData[v].Tint.B, draw.VertexData[v].Tint.A));
                GL.Vertex3(draw.VertexData[v].Position.X, draw.VertexData[v].Position.Y, draw.VertexData[v].Position.Z);
                GL.TexCoord(new Vector2(draw.VertexData[v].TextureCoordinate.X, draw.VertexData[v].TextureCoordinate.Y));
            }

            GL.End();
            GL.PopMatrix();
        }
    }

    public bool OpenGlDraw;

    //void OnPostRender()
    //{
    //    if(OpenGlDraw)
    //    DrawATriangle(mat);
    //}

    //private void OnRenderImage(RenderTexture source, RenderTexture destination)
    //{
        
    //}
}
